﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using KonyvKolcsonzes;

namespace Konyvkolcsonzo
{
    struct iro
    {
        public long id;
        public string nev;
    }

    struct mufaj
    {
        public long id;
        public string nev;
    }

    struct kiado
    {
        public long id;
        public string nev;
        public string cim;
        public string tel;
    }

    class Adatok : DAL
    {
        public List<iro> getIro(string keresoBetu)
        {
            openConnection();
            string error = "";
            List<iro> nevsor = new List<iro>();
            SqlDataReader reader = executeReader("select IroID, IroNev from Irok where IroNev like '%" + keresoBetu + "%'", ref error);
            while (reader.Read())
            {
                iro k;
                k.id = Convert.ToInt32(reader["IroID"]);
                k.nev = Convert.ToString(reader["IroNev"]);
                nevsor.Add(k);
            }
            closeConnection();
            return nevsor;
        }

        public List<mufaj> getMufaj(string keresoBetu)
        {
            openConnection();
            string error = "";
            List<mufaj> nevsor = new List<mufaj>();
            SqlDataReader reader = executeReader("select TipusID, TipusNev from Tipusok where TipusNev like '%" + keresoBetu + "%'", ref error);
            while (reader.Read())
            {
                mufaj k;
                k.id = Convert.ToInt32(reader["TipusID"]);
                k.nev = Convert.ToString(reader["TipusNev"]);
                nevsor.Add(k);
            }
            closeConnection();
            return nevsor;
        }

        public List<kiado> getKiado(string keresoBetu)
        {
            openConnection();
            string error = "";
            List<kiado> nevsor = new List<kiado>();
            SqlDataReader reader = executeReader("select KiadoID, KiadoNev, KiadoCim, KiadoTelSzam from kiadok where kiadoNev like '%" + keresoBetu + "%'", ref error);
            while (reader.Read())
            {
                kiado k;
                k.id = Convert.ToUInt32(reader["KiadoID"]);
                k.nev = Convert.ToString(reader["KiadoNev"]);
                k.cim = Convert.ToString(reader["KiadoCim"]);
                k.tel = Convert.ToString(reader["KiadoTelSzam"]);
                nevsor.Add(k);
            }
            closeConnection();
            return nevsor;
        }

        public string insertIrok(iro k)
        {
            openConnection();
            string error = executeInsertCommand("INSERT into Irok (IroNev) values  ('" + k.nev + "')");
            closeConnection();
            return error;
        }

        public string updateIro(iro k)
        {
            delKonyvIro(k.id);//kell ez a torles?
            openConnection();
            string error = executeUpdateCommand("update Irok set IroNev = '" + k.nev + "' where IroID = '" + k.id + "' ");
            closeConnection();
            return error;
        }

        public string deleteIro(long id)
        {
            delKonyvIro(id); //??
            openConnection(); 
            string error = executeDeleteCommand("delete from Irok where IroID = " + id);
            closeConnection();
            return error;
        }

        public string insertTipus(mufaj k)
        {
            openConnection();
            string error = executeInsertCommand("INSERT into Tipusok (TipusNev) values  ('" + k.nev + "')");
            closeConnection();
            return error;
        }

        public string updateTipus(mufaj k)
        {
            delKonyvTipus(k.id);
            openConnection();
            string error = executeUpdateCommand("update Tipusok set TipusNev = '" + k.nev + "' where TipusID = '" + k.id + "' ");
            closeConnection();
            return error;
        }

        public string deleteTipus(long id)
        {
            delKonyvTipus(id);
            openConnection();
            string error = executeDeleteCommand("delete from Tipusok where TipusID = " + id);
            closeConnection();
            return error;
        }

        public string insertKiado(kiado k)
        {
            openConnection();
            string error = executeInsertCommand("INSERT into Kiadok (KiadoNev, KiadoCim, KiadoTelSzam) values  ('" + k.nev + "', '" + k.cim + "', '" + k.tel + "')");
            closeConnection();
            return error;
        }

        public string updateKiado(kiado k)
        {
            openConnection();
            string error = executeUpdateCommand("update Kiadok set KiadoNev = '" + k.nev + "', KiadoCim = '" + k.cim + "', KiadoTelSzam = '" + k.tel + "' where KiadoID = '" + k.id + "' ");
            closeConnection();
            return error;
        }

        public string deleteKiado(long id)
        {
            delKikolcs(id);
            delPeldanyKiado(id);
            openConnection();
            string error = executeDeleteCommand("delete from Kiadok where KiadoID = " + id);
            closeConnection();
            return error;
        }

        public void delKikolcs(long id) {
            string err = "";
            List<long> nevsor = new List<long>();
            openConnection();
            SqlDataReader reader = executeReader("select PeldanyID from Peldany where KiadoID = " + id, ref err);
            while (reader.Read())
            {
                nevsor.Add(Convert.ToInt32(reader["PeldanyID"]));
            }
            closeConnection();

            for (int i = 0; i < nevsor.Count; i++)
            {
                openConnection();
                string error = executeDeleteCommand("delete from Kikolcsonozve where PeldanyID = " + nevsor[i]);
                closeConnection();
            }
        }

        public string delPeldanyKiado(long id)
        {
            openConnection();
            string error = executeDeleteCommand("delete from Peldany where KiadoID = " + id);
            closeConnection();
            return error;
        }

        public string delKonyvIro(long id)
        {
            openConnection();
            string error = executeDeleteCommand("delete from Iro where IroId = " + id);
            closeConnection();
            return error;
        }

        public string delKonyvTipus(long id)
        {
            openConnection();
            string error = executeDeleteCommand("delete from Tipus where TipusId = " + id);
            closeConnection();
            return error;
        }
    }
}
